import {generateElement} from "../functions/common.js";
import MyComponent from "./MyComponent.js";
import {Point, Triangle} from "./Shape.js";

export default class DiskCanvas extends MyComponent{
    constructor(width, height, config) {
        super();
        this.width = width;
        this.height = height;
        this.elem = generateElement({
            type: "canvas",
            ...config
        });
        this.elem.width = width;
        this.elem.height = height;
        this.basePoint = new Point(0,0);
        this.delta_x = 1;
        this.delta_y = 1;
        this.canvas_ctx = this.elem.getContext("2d");
    }

    set_base(x, y){
        this.basePoint = new Point(x, y);
    }

    set_delta_x(value){
        this.delta_x = value;
    }

    set_delta_y(value){
        this.delta_y = value;
    }

    clear_all () {
        this.canvas_ctx.clearRect(0, 0, this.width, this.height);
    }

    draw_a_line(point1, point2) {
        if (point1 instanceof Point && point2 instanceof Point){
            this.canvas_ctx.beginPath();
            const base_point_x = this.basePoint.x;
            const base_point_y = this.basePoint.y;
            const delta_x = this.delta_x;
            const delta_y = this.delta_y;
            this.canvas_ctx.moveTo(base_point_x + point1.x*delta_x, base_point_y + point1.y*delta_y);
            this.canvas_ctx.lineTo(base_point_x + point2.x*delta_x, base_point_y + point2.y*delta_y);
            this.canvas_ctx.stroke();
        }
    }

    draw_joint_lines(list_of_point){
        if (list_of_point instanceof Array){
            const base_point_x = this.basePoint.x;
            const base_point_y = this.basePoint.y;
            const delta_x = this.delta_x;
            const delta_y = this.delta_y;
            this.canvas_ctx.beginPath();
            this.canvas_ctx.moveTo(base_point_x + list_of_point[0].x*delta_x, base_point_y + list_of_point[0].y*delta_y);
            for (let i = 1;i < list_of_point.length; i++){
                this.canvas_ctx.lineTo(base_point_x + list_of_point[i].x*delta_x, base_point_y + list_of_point[i].y*delta_y);
            }
            this.canvas_ctx.stroke();
        }
    }

    draw_arc(x, y, r, begin, end){
        this.canvas_ctx.beginPath();
        this.canvas_ctx.arc(x, y, r, begin*Math.PI, end* Math.PI);
        this.canvas_ctx.stroke();
    }

    // design for the disk scheduling power point
    draw_point_with_circle(list_of_point, r){
        const base_point_x = this.basePoint.x;
        const base_point_y = this.basePoint.y;
        const delta_x = this.delta_x;
        const delta_y = this.delta_y;
        list_of_point.map((point)=>{
            if (point.mark===true) {
                this.draw_arc(base_point_x + point.x * delta_x, base_point_y + point.y * delta_y, r, 0, Math.PI);
            }
        })
    }

    transAddressX2Px(point){
        return point.x * this.delta_x + this.basePoint.x;
    }

    transAddressY2Px(point){
        return point.y * this.delta_y + this.basePoint.y;
    }

    // draw a triangle
    draw_triangle(triangle, config) {
        if (triangle instanceof Triangle) {
            this.canvas_ctx.beginPath();
            this.canvas_ctx.moveTo(this.transAddressX2Px(triangle.point1), this.transAddressY2Px(triangle.point1));
            this.canvas_ctx.lineTo(this.transAddressX2Px(triangle.point2), this.transAddressY2Px(triangle.point2));
            this.canvas_ctx.lineTo(this.transAddressX2Px(triangle.point3), this.transAddressY2Px(triangle.point3));
            this.canvas_ctx.closePath();
            this.canvas_ctx.fillStyle = "red";
            if (config){
                if (config.fillStyle)this.canvas_ctx.fillStyle = config.fillStyle;
            }
            this.canvas_ctx.fill();
        }
    }

    // draw a text
    draw_text({x, y, text, font}){
        if (font)this.canvas_ctx.font = font;
        this.canvas_ctx.fillText(text, this.transAddressX2Px({x, y}),this.transAddressY2Px({x, y}));
    }
}
